package com.lxw.nonghe.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lxw.nonghe.annotation.RequiresPermission;
import com.lxw.nonghe.common.Result;
import com.lxw.nonghe.entity.ChronicDiseasePolicy;
import com.lxw.nonghe.service.ChronicDiseasePolicyService;
import com.lxw.nonghe.util.LogUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

/**
 * 慢病政策控制器
 */
@RestController
@RequestMapping("/chronicDiseasePolicy")
@RequiresPermission("policy:manage")
public class ChronicDiseasePolicyController {

    @Autowired
    private ChronicDiseasePolicyService policyService;

    @Autowired
    private LogUtils logUtils;

    // 公共的类型转换方法
    private ChronicDiseasePolicy convertPolicy(ChronicDiseasePolicy policy) {
        if (policy.getCapAmount() != null) {
            policy.setCapAmount(new BigDecimal(policy.getCapAmount().toString()));
        }
        if (policy.getReimbursementRate() != null) {
            policy.setReimbursementRate(new BigDecimal(policy.getReimbursementRate().toString()));
        }
        return policy;
    }

    // 公共的日志记录方法
    private void recordLog(String module, String operation, String result, String details) {
        logUtils.recordLog(module, operation, result, details);
    }

    @PostMapping("/create")
    public Result<Boolean> addPolicy(@RequestBody ChronicDiseasePolicy policy) {
        try {
            policy = convertPolicy(policy);
            boolean result = policyService.save(policy);
            if (result) {
                recordLog("慢病政策管理", "新增政策", "成功", "疾病:" + policy.getDiseaseName() + ", 年度:" + policy.getYear());
                return Result.success("慢病政策新增成功", true);
            } else {
                recordLog("慢病政策管理", "新增政策", "失败", "保存数据库失败");
                return Result.error("慢病政策新增失败");
            }
        } catch (Exception e) {
            recordLog("慢病政策管理", "新增政策", "失败", e.getMessage());
            return Result.error("系统异常: " + e.getMessage());
        }
    }

    @PostMapping("/delete/{id}")
    public Result<Boolean> deletePolicy(@PathVariable Integer id) {
        try {
            ChronicDiseasePolicy policy = policyService.getById(id);
            boolean result = policyService.removeById(id);
            if (result) {
                recordLog("慢病政策管理", "删除政策", "成功", "政策ID:" + id + ", 疾病:" + (policy != null? policy.getDiseaseName() : "未知"));
                return Result.success("慢病政策删除成功", true);
            } else {
                recordLog("慢病政策管理", "删除政策", "失败", "政策ID:" + id + "不存在");
                return Result.error("慢病政策删除失败");
            }
        } catch (Exception e) {
            recordLog("慢病政策管理", "删除政策", "失败", e.getMessage());
            return Result.error("系统异常: " + e.getMessage());
        }
    }

    @PostMapping("/update")
    public Result<Boolean> updatePolicy(@RequestBody ChronicDiseasePolicy policy) {
        try {
            policy = convertPolicy(policy);
            boolean result = policyService.updateById(policy);
            if (result) {
                recordLog("慢病政策管理", "更新政策", "成功", "政策ID:" + policy.getPolicyId());
                return Result.success("慢病政策更新成功", true);
            } else {
                recordLog("慢病政策管理", "更新政策", "失败", "政策ID:" + policy.getPolicyId() + "不存在");
                return Result.error("慢病政策更新失败");
            }
        } catch (Exception e) {
            recordLog("慢病政策管理", "更新政策", "失败", e.getMessage());
            return Result.error("系统异常: " + e.getMessage());
        }
    }

    @GetMapping("/get/{id}")
    public Result<ChronicDiseasePolicy> getPolicyById(@PathVariable Integer id) {
        try {
            ChronicDiseasePolicy policy = policyService.getById(id);
            if (policy != null) {
                /*recordLog("慢病政策管理", "查询政策详情", "成功", "政策ID:" + id);*/
                return Result.success("慢病政策查询成功", policy);
            } else {
                recordLog("慢病政策管理", "查询政策详情", "失败", "政策ID:" + id + "不存在");
                return Result.error("未找到该慢病政策");
            }
        } catch (Exception e) {
            recordLog("慢病政策管理", "查询政策详情", "失败", e.getMessage());
            return Result.error("查询异常: " + e.getMessage());
        }
    }

    @GetMapping("/getByDisease/{diseaseName}")
    public Result<List<ChronicDiseasePolicy>> getPolicyByDiseaseName(@PathVariable String diseaseName) {
        try {
            List<ChronicDiseasePolicy> policyList = policyService.getChronicDiseasePolicyByDiseaseName(diseaseName);
            if (policyList != null) {
                /*recordLog("慢病政策管理", "按疾病查询政策", "成功", "疾病名称:" + diseaseName);*/
                return Result.success("慢病政策查询成功", policyList);
            } else {
                recordLog("慢病政策管理", "按疾病查询政策", "失败", "疾病名称:" + diseaseName + "无政策");
                return Result.error("未找到该疾病的政策");
            }
        } catch (Exception e) {
            recordLog("慢病政策管理", "按疾病查询政策", "失败", e.getMessage());
            return Result.error("查询异常: " + e.getMessage());
        }
    }

    @GetMapping("/list")
    public Result<Page<ChronicDiseasePolicy>> getPolicyPage(
            @RequestParam(defaultValue = "1") int current,
            @RequestParam(defaultValue = "10") int size,
            @RequestParam(required = false) String diseaseName,
            @RequestParam(required = false) String diseaseCode,
            @RequestParam(required = false) String policyName,
            @RequestParam(required = false) Byte status) {
        // 检查参数合法性
        if (current <= 0) {
            current = 1;
        }
        if (size <= 0) {
            size = 10;
        }

        Page<ChronicDiseasePolicy> page = policyService.getPolicyPage(current, size, diseaseName, diseaseCode, policyName, status);
        return Result.success("查询成功", page);
    }

    @PostMapping("/query")
    public Result<Page<ChronicDiseasePolicy>> queryPolicies(
            @RequestBody ChronicDiseasePolicy queryParam,
            @RequestParam(defaultValue = "1") int current,
            @RequestParam(defaultValue = "10") int size) {
        try {
            // 检查参数合法性
            if (current <= 0) {
                current = 1;
            }
            if (size <= 0) {
                size = 10;
            }

            Page<ChronicDiseasePolicy> page = policyService.queryChronicDiseasePolicy(queryParam, current, size);
            /*recordLog("慢病政策管理", "条件查询政策", "成功", "条件:" + queryParam.toString() + ", 页码:" + current + ", 大小:" + size);*/
            return Result.success("慢病政策条件查询成功", page);
        } catch (Exception e) {
            recordLog("慢病政策管理", "条件查询政策", "失败", e.getMessage());
            return Result.error("条件查询异常: " + e.getMessage());
        }
    }
}
